<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 20:37
 */

namespace app\admin\model;

use think\facade\Session;

class ApplyWith extends \think\Model
{
    const ApplyWith_TYPE = [
        1 => '支付宝',
        2 => '微信',
    ];

    /**
     * 获取单条
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     */
    public function _findOne($id){
        return $this->where([['id', '=', $id]])->find();
    }


    /**
     * 列表
     * @param array $condition
     * @param int $page_size
     * @param string $order_by
     * @return \think\Paginator
     */
    public function _list($condition = [], $page_size = PAGE_SIZE_ADMIN, $order_by = 'created desc'){
        $obj = $this->alias('a')
            ->field('a.*, m.user_name')
            ->join('member m', 'm.id = a.mid', 'LEFT')
            ->order($order_by);
        $result = get_page($obj, $condition, $page_size);
       
        return $result;
    }

 
    /**
    * 同意
    * @param id int 记录id
    * @param op_id int 后台人员id
    * @return bool true成功 false失败
    */
    public function _agree($id, $op_id){
        $data = $this->_findOne($id);
        if(empty($data)){
            $this->error = '数据不存在';
            return false;
        }
        if($data['status'] != 1){
            $this->error = '数据信息已改变';
            return false;
        }
        
        $update = [
            'status' => 2,
            'op_id' => $op_id,
            'op_time' => time(),
            'remark' => '提现到支付宝成功',
        ];
        
        $where = [
            'id' => $id,
            'status' => 1,
        ];
        $ret = $this->save($update, $where);
        if(!$ret){
            $this->error = '审核失败';
        }
        return true;
    }

    /**
    * 拒绝
    * @param id int 记录id
    * @param op_id int 后台人员id
    * @return bool true成功 false失败
    */
    public function _refuse($id, $op_id){
        $data = $this->_findOne($id);
        if(empty($data)){
            $this->error = '数据不存在';
            return false;
        }
        if($data['status'] != 1){
            $this->error = '数据信息已改变';
            return false;
        }
        
        $update = [
            'status' => -1,
            'op_id' => $op_id,
            'op_time' => time(),
            'remark' => '提现到支付宝成功',
        ];
        
        $where = [
            'id' => $id,
            'status' => 1,
        ];
        $ret = $this->save($update, $where);
        if(!$ret){
            $this->error = '审核失败';
        }
        return true;
    }


    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function _remove($id){
        $result = $this->_findOne($id);
        if(!$result){
            $this->error = '无此';
            return false;
        }
        //var_dump($result->is_can_del);die;
        if($result->is_can_del == -1){
            $this->error = '该禁止删除';
            return false;
        }
        if(!$result->delete()){
            $this->error = '操作失败';
            return false;
        }
        
        return true;
    }
    /**
     * 批量删除
     * @param $id
     * @return bool
     */
    public function _removeAll($id){
        $ret = $this->where([['id', 'in', $id]])->delete();
        if(!$ret){
            $this->error = '操作失败';
            return false;
        }
        
        return true;
    }
    
    /**
    * 数据总条数
    */
    public function _count($where = []){
        return $this->alias('u')->where($where)->count();
    }

    /**
    * 获取条件
    */
    public function _where($request){
        $p = isset($request['page']) ? $request['page'] : 1;
        $start = isset($request['start']) ? $request['start'] : '';
        $end = isset($request['end']) ? $request['end'] : '';
        $type = isset($request['type']) ? $request['type'] : '';
        $title = isset($request['title']) ? $request['title'] : '';
        $status = isset($request['status']) ? $request['status'] : '';
        
        $request['page'] = $p;
        $request['start'] = $start;
        $request['end'] = $end;
        $request['title'] = $title;
        $request['type'] = $type;
        $request['status'] = $status;
        
        $search = $request;
        
        $title = htmlspecialchars(addslashes(str_replace(' ', '', $request['title'])));
        $request['title'] = str_replace('%', "\%", $title);
        
        $tmp = $request;
        foreach($tmp as $k=>$v){
            if(empty($v)){
                unset($tmp[$k]);
            }
        }
        
        $where = '';
        foreach($tmp as $k=>$v){
            if($k == 'start'){
                $start = strtotime($v);
                $where .= "a.created >= $start and ";
            }elseif($k == 'a.end'){
                $end = strtotime($v) + 24*3600;
                $where .= "a.created <= $end and ";
            }elseif($k == 'title'){
                $where .= "m.user_name like '%$v%' and ";
            }elseif($k == 'type'){
                $where .= "a.type_id = $v and ";
            }elseif($k == 'status'){
                $where .= "a.status = $v and ";
            }
        }
        if(!empty($where)){
            $where = substr($where, 0, -4);
        }
        return array('search'=>$search,'where'=>$where);
    }
}